import React, { FC, useCallback, memo } from 'react';
import styles from './autoRefreshSwitch.module.less';
import { message, Switch } from 'antd';
import Base from '@/components/auto-refresh-switch/Base';

interface IAutoRefreshSwitchProps {
  loading?: boolean;
  checked: boolean;
  handleAutoRefreshChange: (flag: boolean) => void;
}

const AutoRefreshSwitch: FC<IAutoRefreshSwitchProps> = (props) => {
  const { checked, handleAutoRefreshChange, loading } = props;

  const handleChange = useCallback(
    (flag: boolean) => {
      handleAutoRefreshChange && handleAutoRefreshChange(flag);
      message.success(flag ? '已开启自动刷新' : '已关闭自动刷新').then();
    },
    [handleAutoRefreshChange],
  );
  return (
    <Base
      text="自动刷新："
      checked={checked}
      onChange={handleChange}
      loading={loading}
    />
  );
};

export default memo(AutoRefreshSwitch);
